package com.example.eduservice.controller;


import com.example.commonutils.Result;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Anticipate
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    // 课程大纲列表，根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("allChapterVideoList", list);
    }

    // 添加章节
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);
        return Result.ok();
    }

    // 根据章节id查询
    @GetMapping("getChapterInfoById/{chapterId}")
    public Result getChapterInfoById(@PathVariable String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter", chapter);
    }

    // 修改章节
    @PostMapping("updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return Result.ok();
    }

    /*
    * 删除章节
    * 如果章节下面有小节 则不允许删除 必须先将章节里面的小节删除了才能删除章节
    * 无小节可以允许删除章节
    *
    * */
    @DeleteMapping("deleteChapter/{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId) {
        chapterService.deleteChapter(chapterId);
        return Result.ok();
    }

}

